window.onload = function () {
    cutAll(1);
}

/**
 * 分页显示
 * @param pageNo
 */
function cutAll(pageNo) {
    axios.get("/order/cutAll", {
        params: {
            pageNo: pageNo
        }
    }).then(resp => {
        var info = resp.data;
        var str = "";
        info.list.forEach(n => {
            str += `<tr><td>${n.userName}</td><td>${n.orderDate}</td><td>${n.address}</td>
                <td><a href="javascript:del(${n.id})">删除</a><a href="javascript:findById(${n.id})">修改</a></td>
                  
             </tr>`;
        });
        $("data").innerHTML = str;
        var cutStr = "";
        for (var i = 1; i <= info.totalPage; i++) {
            cutStr += `<a href="javascript:cutAll(${i})" style="margin-left: 10px">${i}</a>`;
        }
        $("cutDiv").innerHTML = cutStr;
    });

}

function $(id) {
    return document.getElementById(id)
}

/**
 *
 */
function showAddData() {
    $("addDiv").style.visibility = "visible"
}

// function  addOrder(){
//     axios.get("/order/add",{
//         params: {
//             userName: $("userName").value,
//             address:$("address").value
//         }
//     }).then(resp=>{
//         var info=resp.data;
//         if (info=="ok"){
//             cutAll(1);
//             $("userName").value = null
//             $("address").value = null
//             $("addDiv").style.visibility = "hidden"
//         }
//     })
// }

let paramobj = new URLSearchParams();
paramobj.append("userName", $("userName").value);
paramobj.append("address", $("address").value);
//向服务器发送ajks的post请求
function addOrder() {
    axios.post("/order/add", paramobj).then(
        resp => {
            var info = resp.data;
            if (info == "ok") {
                cutAll(1);
                $("userName").value = null
                $("address").value = null
                $("addDiv").style.visibility = "hidden"
            }
        }
    )

}


function del(id) {
    axios.get("/order/del", {
        params: {
            id
        }
    }).then(resp => {
        var info = resp.data;
        if (info = "ok") {
            cutAll(1)
        }
    })
}

function findById(id) {
    axios.get("/order/findById", {
        params: {
            id
        }
    }).then(resp => {
        var info = resp.data;
        $("nameSpan").innerHTML = info.userName;
        $("updateAddress").value = info.address;
        $("id").value = info.id;

        $("updateDiv").style.visibility = "visible";
    })
}

function updateOrder(id) {
    axios.get("/order/update", {
        params: {
            id: $("id").value,
            address: $("updateAddress").value
        }
    }).then(resp => {
        var info = resp.data;
        if (info == "ok") {
            cutAll(1);
            $("updateDiv").style.visibility = "hidden"
        }
    })
}